package com.server.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author e4glet
 * 2025年3月5日
 * 数据库连接工具类
 * */

public class DBUtils {
    private static final String URL = "jdbc:mysql://localhost:3306/oicqdatabase";
    private static final String USER = "root";
    private static final String PASSWORD = "x5";

    // 静态初始化块，加载MySQL驱动
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //添加数据库连接方法，Open()
    public static Connection Open(){
        //新建数据库连接对象 conn
        Connection conn =null;
        try {
            //通过驱动管理器，调用getConnection方法，尝试连接MySQL
            conn=(Connection) DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }
}
